import io.github.palexdev.materialfx.controls.MFXButton;
import io.github.palexdev.materialfx.controls.MFXTableColumn;
import io.github.palexdev.materialfx.controls.MFXTableRow;
import io.github.palexdev.materialfx.controls.MFXTableView;
import io.github.palexdev.materialfx.controls.cell.MFXTableRowCell;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import lombok.Data;

import java.util.function.Function;

public class TableTest2 extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {

        MFXTableView<PlayItem> tableView = new MFXTableView<>();
        ObservableList<MFXTableColumn<PlayItem>> tableColumns = tableView.getTableColumns();

        MFXTableColumn<PlayItem> id = new MFXTableColumn<>("id");
        id.setAlignment(Pos.CENTER);
        tableColumns.add(id);

        MFXTableColumn<PlayItem> name = new MFXTableColumn<>("name");
        name.prefWidth(200);
        name.setMinWidth(200);
        tableColumns.add(name);

        MFXTableColumn<PlayItem> url = new MFXTableColumn<>("url");
        url.setAlignment(Pos.CENTER);
        tableColumns.add(url);

        MFXTableColumn<PlayItem> button = new MFXTableColumn<>("btn");
        button.setPrefWidth(200);
        button.minWidth(200);
        tableColumns.add(button);

        tableView.setTableRowFactory(new Function<PlayItem, MFXTableRow<PlayItem>>() {
            @Override
            public MFXTableRow<PlayItem> apply(PlayItem playItem) {
                MFXTableRow<PlayItem> row = new MFXTableRow<>(tableView,playItem);
                row.getCells().add(new MFXTableRowCell<>(null));
                return row;
            }
        });

        ObservableList<PlayItem> list = FXCollections.observableArrayList();
        Image image = new Image("https://p4.music.126.net/4K7h0kwxj75VbSMYsZXLrQ==/6012129580679384.jpg?param=34y34");
        for (int i = 0; i < 100; i++) {
            MFXButton mfxButton = new MFXButton("", new ImageView(image)) {
                @Override
                public String toString() {
                    return "123";
                }
            };
            list.add(new PlayItem(i, "name" + i, "url" + i, mfxButton));
        }
        tableView.setItems(list);
        tableView.setId("tb");
        tableView.getStylesheets().add(TableTest2.class.getResource("/css/test.css").toExternalForm());

        AnchorPane.setLeftAnchor(tableView, 0.0);
        AnchorPane.setRightAnchor(tableView, 0.0);
        AnchorPane.setTopAnchor(tableView, 0.0);
        AnchorPane.setBottomAnchor(tableView, 0.0);

        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setPrefSize(1280, 720);
        anchorPane.getChildren().add(tableView);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.show();
    }

    @Data
    static class PlayItem {
        private final int id;
        private final String name;
        private final String url;
        private final MFXButton mfxButton;
    }
}
